﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using PccCommonForC;

public partial class Common_Download_Download : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {            
            BindData();
        }

    }

    private void BindData()
    {
        DataTable dt = new DataTable();
       
        clsWebShop cls = new clsWebShop();
        PccMsg msg = new PccMsg();
        msg.CreateFirstNode("IdD","0");
        msg.CreateFirstNode("Status", "N");
        dt = cls.Get_DownloadByCondition(msg.GetXmlStr).Tables[0];
        grvNews.DataSource = dt;
        grvNews.DataBind();

        if (dt.Rows.Count > 0)
        {
            DataTable dtD = new DataTable();
            dtD = cls.GetData("select * from DowloadDetail order by UpdDate desc").Tables[0];

            for (int i = 0; i < grvNews.Rows.Count; i++)
            {
                DataList dtlFile = (DataList)grvNews.Rows[i].FindControl("dtlFile");
                DataView dv=new DataView();
                dv = dtD.DefaultView;
                dv.RowFilter = " IdD="+ grvNews.DataKeys[i].Value.ToString();
                if (dtD.Rows.Count > 0)
                {
                    dtlFile.DataSource = dv;
                    dtlFile.DataBind();
                    for (int j = 0; j < dtlFile.Items.Count; j++)
                    {                        
                            HyperLink hplLink = (HyperLink)dtlFile.Items[j].FindControl("hplLink");
                            HyperLink hplImg = (HyperLink)dtlFile.Items[j].FindControl("hplImg");
                            if (hplLink.NavigateUrl.IndexOf("http://") == -1)//Khong tim thay
                            {
                                hplLink.NavigateUrl = ResolveUrl("~/") + hplLink.NavigateUrl;
                                hplImg.NavigateUrl = ResolveUrl("~/") + hplImg.NavigateUrl;
                            }                        
                    }
                }
            }
        }
       
    }

    protected void grvNews_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label lblDate = (Label)e.Row.FindControl("lblDate");
            lblDate.Text = "Cập nhật ngày: " + FormatDate(lblDate.Text);
        
       }
    }
    private string FormatDate(string sDate)
    {
        string sReturn = "";
        try
        {
            sReturn = sDate.ToString().Substring(6, 2) + "/" + sDate.ToString().Substring(4, 2) + "/" + sDate.ToString().Substring(0, 4);
        }
        catch (Exception ex) { }

        return sReturn;
    }
    protected void grvNews_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grvNews.PageIndex = e.NewPageIndex;
        BindData();
    }
}
